package com.spring.staticadvice;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class GreetingDynamicPointcut extends DynamicMethodMatcherPointcut {

    private static List<String> specialClientList = new ArrayList<String>();

    static {
        specialClientList.add("xiaoan");
        specialClientList.add("lihua");
    }

    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> aClass) {
                System.out.println("调用getClassFilter（）对" + aClass.getName() + "做静态检查");
                return Waiter.class.isAssignableFrom(aClass);

            }
        };
    }

    /**
     * 对方法进行静态切点检查
     * @param method
     * @param targetClass
     * @return
     */
    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        System.out.println("调用match对类的方法做静态检查" + targetClass.getName() + "." + method.getName());
        return "greetTo".equals(method.getName());
    }

    /**
     * 对方法进行动态切点检查
     * @param method
     * @param aClass
     * @param objects
     * @return
     */
    @Override
    public boolean matches(Method method, Class<?> aClass, Object... objects) {

        System.out.println("动态检查");
        return specialClientList.contains(objects);
    }
}
